# 第四章
# 4.3.2
# range函数的第三个参数是步长
even_numbers = list(range(2, 11, 2))
print(even_numbers)

# 4.3.2
squares = []
for value in range(1, 11):
    squares.append(value ** 2)
print(squares)

# 列表推导式写法：
# 列表名 = [表达式 一个循环为表达式提供值]

# 4.3后面的动手试一试
# 练习4.3
for i in range(1, 21):
    print(i)

# 练习4.4
# 下面的程序引入了time模块，time.sleep(seconds)可以使程序暂停运行几秒
"""
import time
numbers = [number for number in range(1, 1_000_001)]
for i in range(0, 1_000_000):
    time.sleep(1)
    print(numbers[i])
"""

# 练习4.5
numbers = [number for number in range(1, 1_000_001)]
print(min(numbers))
print(max(numbers))
print(sum(numbers))

# 练习4.6
numbers = list(range(1, 21, 2))
for i in numbers:
    print(i)

# 练习4.7
numbers = list(range(3, 31, 3))
for i in numbers:
    print(i)

# 练习4.8
cube_number = []
for number in range(1, 11):
    cube_number.append(number ** 3)
for i in cube_number:
    print(i)

# 练习4.9
cube_number = [number ** 3 for number in range(1, 11)]
for i in cube_number:
    print(i)


# 4.4.3-前
# 复制列表中，如果执行的是切片后赋值，则将形成一个新的列表
three_alphabet = ['a', 'b', 'c']
new_list = three_alphabet[:]

three_alphabet.append('d')
new_list.append('e')
print(three_alphabet)
print(new_list)

# 4.4.3-后
# 复制列表中，如果执行的是变量名赋值，则只是两个变量名指向同一个列表
# 以下程序的结果只能打印两遍['a', 'b', 'c', 'd', 'e']
three_alphabet = ['a', 'b', 'c']
new_list = three_alphabet

three_alphabet.append('d')
new_list.append('e')

print(three_alphabet)
print(new_list)

# 4.4后面的动手试一试
# 练习4.10
even_numbers = list(range(2, 11, 2))
first_three_number = even_numbers[:3]
print(f'The first three items in the list are: {first_three_number}')

middle_three_number = even_numbers[1:4]
print(f'Three items from the middle of the list are: {middle_three_number}')

last_three_number = even_numbers[-3:]
print(f'The last three items in the list are: {last_three_number}')

# 练习4.11
three_alphabet = ['a', 'b', 'c']
new_list = three_alphabet[:]
three_alphabet.append('d')
new_list.append('e')
for alphabet in three_alphabet:
    print(alphabet, end=' ')

print('\n')
for alphabet in new_list:
    print(alphabet, end=' ')

